<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>菜单</title>
    <link rel="stylesheet" href="/lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="/css/public.css" media="all">
    <style>
        .layui-btn:not(.layui-btn-lg ):not(.layui-btn-sm):not(.layui-btn-xs) {
            height: 34px;
            line-height: 34px;
            padding: 0 8px;
        }
    </style>
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <!-- <blockquote class="layui-elem-quote">
             Layui的树形表格treeTable，支持异步加载(懒加载)、复选框联动、折叠状态记忆。<br>
             <a href="https://gitee.com/whvse/treetable-lay" target="_blank" class="layui-btn layui-btn-danger">treetable-lay</a>
         </blockquote>-->
        <div>
            <div class="layui-btn-group">
                <button class="layui-btn" id="btn-expand">全部展开</button>
                <button class="layui-btn layui-btn-normal" id="btn-fold">全部折叠</button>
            </div>
            <table id="munu-table" class="layui-table" lay-filter="munu-table"></table>
        </div>
    </div>
</div>
<!-- 操作列 -->
<script type="text/html" id="auth-state">
    <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="add">
        <i class="layui-icon layui-icon-addition"></i>添加</a>
    <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">
        <i class="layui-icon layui-icon-edit"></i>编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">
        <i class="layui-icon layui-icon-delete"></i>删除</a>
</script>

<script src="/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script src="/js/lay-config.js" charset="utf-8"></script>
<script th:inline="none">
    layui.use(['table', 'treetable', 'common'], function () {
        let $ = layui.jquery;
        let table = layui.table;
        let treetable = layui.treetable;

        // 渲染表格
        layer.load(2);
        treetable.render({
            treeColIndex: 1,
            treeSpid: 0,
            treeIdName: 'id',
            treePidName: 'parentId',
            elem: '#munu-table',
            url: '/admin/resource/table',
            page: false,
            cols: [[
                {type: 'numbers', title: '序号', fixed: 'left'},
                {field: 'resourceName', minWidth: 200, title: '资源名称'},
                {field: 'icon', title: '图标'},
                {field: 'permission', title: '权限标识'},
                {field: 'url', title: '资源url'},
                // {field: 'sort', width: 80, align: 'center', title: '排序号'},
                {
                    field: 'type', width: 80, align: 'center', templet: function (d) {
                        if (d.type == 1) {
                            return '<span class="layui-badge layui-bg-blue">目录</span>';
                        }
                        if (d.type == 2) {
                            return '<span class="layui-badge-rim">菜单</span>';
                        } else {
                            return '<span class="layui-badge layui-bg-gray">按钮</span>';
                        }
                    }, title: '类型'
                },
                {templet: '#auth-state', width: 220, align: 'center', title: '操作'}
            ]],
            done: function () {
                layer.closeAll('loading');
            }
        });

        $('#btn-expand').click(function () {
            treetable.expandAll('#munu-table');
        });

        $('#btn-fold').click(function () {
            treetable.foldAll('#munu-table');
        });

        //监听工具条
        table.on('tool(munu-table)', function (obj) {
            let data = obj.data;
            let layEvent = obj.event;
            if (layEvent === 'del') {
                layer.confirm('确定操作', function (index) {
                    layer.close(index);
                    $.ajax({
                        url: '/admin/resource?id=' + data.id,
                        type: 'delete',
                        success: function (res) {
                            if (res.code == 200) {
                                layer.msg('操作成功', {time: 1000});
                                window.location.reload();
                            }
                        },
                    });
                });
            } else if (layEvent === 'edit') {
                let index = layer.open({
                    title: '编辑资源',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['50%', '50%'],
                    content: '/route/admin/resource/edit?id=' + data.id,
                    zIndex: layer.zIndex,
                    success: function (layero, index) {
                        window[layero.find("iframe")[0]["name"]].back = function (cb) {
                            cb && cb();
                            window.location.reload();
                            layer.close(index);
                        };
                    },
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });

            } else if (layEvent === 'add') {
                let index = layer.open({
                    title: '添加资源',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['50%', '50%'],
                    content: '/route/admin/resource/add?id=' + data.id + "&parentId=" + data.parentId + "&type=" + data.type,
                    zIndex: layer.zIndex,
                    success: function (layero, index) {
                        window[layero.find("iframe")[0]["name"]].back = function (cb) {
                            cb && cb();
                            window.location.reload();
                            layer.close(index);
                        };
                    },
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            }
        });
    });
</script>
</body>
</html>